package cuiyt.algorithm.dfs;


import cuiyt.algorithm.TreeNode;

/**
 * @author cyt
 * @create 2020-08-25 8:54
 */
public class T0402 {
    public static void main(String[] args) {

    }

    /**
     * 给定一个有序整数数组，元素各不相同且按升序排列，编写一个算法，创建一棵高度最小的二叉搜索树。
     *
     * @param nums
     * @return
     */
    public TreeNode sortedArrayToBST(int[] nums) {
        TreeNode dfs = dfs(nums, 0, nums.length-1);
        return dfs;
    }

    public TreeNode dfs(int[] nums, int l, int r) {
        if (l > r) {
            return null;
        }
        if(l == r){
            return new TreeNode(nums[l]);
        }
        int mid = (r + l) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = dfs(nums, l, mid-1);
        node.right = dfs(nums, mid + 1, r);
        return node;
    }

}
